package algorithms.a08MinimumSpanTrees;

/**
 * 边
 * 带权重的边
 *
 * @author 王昊然
 * @create 2018-03-05 16:06
 **/
public class Edge<Weight extends Number & Comparable> implements Comparable<Edge>{
    private int a, b;
    private Weight weight;

    public Edge(int a, int b, Weight weight) {
        this.a = a;
        this.b = b;
        this.weight = weight;
    }

    //该构造函数意在将一个边转为新的边对象
    public Edge(Edge<Weight> e){
        this.a = e.V();
        this.b = e.W();
        this.weight = e.wt();
    }

    public int V() {
        return a;
    }

    public int W() {
        return b;
    }

    public Weight wt() {
        return weight;
    }

    public int other(int x) {
        assert (x == a || x == b);
        return x == a ? b : a;
    }

    public String toString() {
        return "" + a + "-" + b + ": " + weight;
    }

    // 边之间的比较
    @Override
    public int compareTo(Edge that) {
        if (weight.compareTo(that.wt()) < 0)
            return -1;
        else if (weight.compareTo(that.wt()) > 0)
            return +1;
        else
            return 0;
    }
}
